<<<<<<< HEAD
<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-27
 * Func   订单开奖
 */
header('Content-Type:text/html; charset=UTF-8');
date_default_timezone_set('PRC');
error_reporting(E_ERROR | E_WARNING | E_PARSE);

define('IN_YuLin', true);
define('TIMESTAMP', time());
define('ROOT_PATH', str_replace('\\', '/', dirname(__FILE__) . '/')); //根目录
define('DATA_PATH', ROOT_PATH . 'data/'); //数据目录
define('LIB_PATH', ROOT_PATH . 'lib/');  //类库文件

include_once LIB_PATH . 'common.inc.php';
require_once LIB_PATH . 'protect.php';
include_once LIB_PATH . 'common.func.php';

//	数据库
include(LIB_PATH.'class/Db.class.php');
$db = new DB($_config['db']);

//	彩票种类
$lottery = $db->getall('SELECT * FROM '.Table('lottery').' WHERE status = 1 ORDER BY listorder DESC','id');

//	操作的数据库
$table = Table('lottery_order_main');
$ltable = Table('lottery_order_item');

$ptable = Table('lottery_period');
$ptable2 = Table('lottery_play');

//	读取快3和时时彩
$list = $db->getall('SELECT id,title,sname FROM '.Table('lottery').' where id in(1,2,3,4,5,6,7,8,9,10,11,12,13)');

foreach($list as $val)
{
	//拿到本期的开奖号
	//$open = openCode($val['sname']);
	//$open && $open = $open['data'][0];

	$open['expect'] = '20180322039';
	$open['opencode'] = '3,3,5';
	$open['opentime'] = '2018-03-22 15:00:49';
	$open['opentimestamp'] = '1521702049';

	/* $arr = array();
	$arr['token'] = 'taeeaf826af0022cak';
	$arr['code'] = $info['sname'];
	$arr['rows'] = '10';
	$arr['format'] = 'json';
	$x = HttpPost('http://ho.apiplus.net/newly.do',$arr);
	$data = json_decode($x,true); */

	//先把开奖号和开奖时间更新到期号表中
	$tmp = [];
	$tmp['opentime'] = $open['opentimestamp'];
	$tmp['code'] = $open['opencode'];
	$tmp['status'] = 1;
	$db->exec('UPDATE '.Table('lottery_k3').''.CreateUpdateSql($tmp).' where period = '.$open['expect']);
	
	//读取本期所有订单	
	//$data = $db->getall('SELECT * FROM '.Table('lottery_order_main').' o LEFT JOIN '.Table('lottery_order_item').' i ON o.id = i.o_id where o.period = '.$open['expect']);
	$data = $db->getall('SELECT o.* FROM '.Table('lottery_order_item').' o LEFT JOIN '.Table('lottery_order_main').' i ON o.o_id = i.id where o.period_id = 20180322067 and i.period is not null');

	//	加载开奖玩法计算
	include_once(LIB_PATH.'lottery/'.$val['sname'].'_prize.inc.php');

	$arr = array('1','2','3','4','5','6','7','8','9','10');
	if(in_array($val['id'],$arr))
	{
		$type = 'k3';
	}else{
		$type = 'ssc';
	}

	//循环传入订单信息计算开奖金额
	$func = 'PrizeCount_'.$type;
	
	foreach($data as $k => $v)
	{
		PrizeCount_k3($v,$open['opencode']);
	}
	//$money = PrizeCount_k3($data);

	die;
	$money = round($money,2);
	if($money > 0)	//	中奖
	{
		//	奖金记录
		$tmp = [];
		$tmp['uid']		=	$val['uid'];
		$tmp['tid']		=	'prize';
		$tmp['lkey']	=	'prize_'.$val['uid'].'_'.$val['id'];
		$tmp['money']	=	$money;
		$tmp['balance']	=	$money;
		$tmp['notes']	=	'彩票中奖：'.$lottery[$val['cid']]['title'].',ID：'.$val['id'];
		$res = MoneyLog($tmp);
		//!$res && AjaxReturn(1,'账户余额不足，请先充值');
	}
	
	//	更新订单状态
	//$db->exec('UPDATE '.$table.' SET prize = '.$money.',prizetime = '.TIMESTAMP.',status = 1 WHERE id = '.$val['id']);
	

}


function openCode($sname){
    $arr           = array();
    $arr['token']  = 'taeeaf826af0022cak';
    // 彩种的简称，这里后期做动态，从数据库中取sname与开奖的周期数，程序按周期来执行某个彩种开奖。
    $arr['code']   = $sname;
    $arr['rows']   = '1';
    $arr['format'] = 'json';
    $x             = HttpPost('http://ho.apiplus.net/newly.do',$arr);
    $data          = json_decode($x,true);
    return $data;
}

=======
<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-27
 * Func   订单开奖
 */
header('Content-Type:text/html; charset=UTF-8');
date_default_timezone_set('PRC');
error_reporting(E_ERROR | E_WARNING | E_PARSE);

define('IN_YuLin', true);
define('TIMESTAMP', time());
define('ROOT_PATH', str_replace('\\', '/', dirname(__FILE__) . '/')); //根目录
define('DATA_PATH', ROOT_PATH . 'data/'); //数据目录
define('LIB_PATH', ROOT_PATH . 'lib/');  //类库文件

include_once LIB_PATH . 'common.inc.php';
require_once LIB_PATH . 'protect.php';
include_once LIB_PATH . 'common.func.php';

//	数据库
include(LIB_PATH.'class/Db.class.php');
$db = new DB($_config['db']);

if($_GET['a'] == 'agent')	//	代理返点
{
	//	每月1号0-6点结算上月
	//((date('d') != 1) || (date('H') > 5)) && exit('end');
	
	//	本月1号和上个月1号
	$dt2 = strtotime(date('Y-m-01'));
	$dt1 = strtotime(date('Y-m-01',strtotime('-1 month')));
	
	$sql = 'SELECT uid FROM '.Table('user_money_rebate').' WHERE dateline > '.$dt2;	//	每个月只统计一次
	$data = $db->getall('SELECT t.id,s.rebate FROM '.Table('user').' t LEFT JOIN '.Table('user_status').' s ON t.id = s.uid WHERE t.gid = 2 AND t.status = 1 AND s.sons > 0 AND s.profit = 1 AND t.id NOT IN ('.$sql.')');
	foreach($data as $val)
	{
		$t2 = $db->getall('SELECT t.id,t.username,t.money FROM '.Table('user').' t LEFT JOIN '.Table('user_status').' s ON t.id = s.uid WHERE s.upuid = '.$val['id'].' AND t.status = 1');
		foreach($t2 as $val2)
		{
			//	查询充值
			$t3 = $db->getrow('SELECT SUM(money) AS p FROM '.Table('user_money_recharge').' WHERE uid = '.$val2['id'].' AND status = 1 AND dateline > '.$dt1.' AND dateline < '.$dt2);
			$recharge = round($t3['p'],2);
			
			//	查询提现
			$t3 = $db->getrow('SELECT SUM(money) AS p FROM '.Table('user_money_cash').' WHERE uid = '.$val2['id'].' AND status = 1 AND dateline > '.$dt1.' AND dateline < '.$dt2);
			$cash = round($t3['p'],2);
			
			//	收入计算
			$money = max(0,round(($recharge - $cash - $val2['money']) * $val['rebate'] / 100,2));
			
			$ins = [];
			$ins['uid']		=	$val['id'];
			$ins['suid']	=	$val2['id'];
			$ins['recharge']=	$recharge;
			$ins['cash']	=	$cash;
			$ins['balance']	=	$val2['money'];
			$ins['money']	=	$money;
			$ins['tid']		=	date('Ymd',$dt1);
			$ins['dateline']=	TIMESTAMP;
			$id = $db->add('user_money_rebate',$ins);
			
			//	资金明细
			if($money > 0)
			{
				$tmp = [];
				$tmp['uid']		=	$val['id'];
				$tmp['tid']		=	'agent';
				$tmp['lkey']	=	'agent_'.$val['id'].'_'.$id;
				$tmp['money']	=	$money;
				$tmp['balance']	=	$money;
				$tmp['notes']	=	'代理分成：'.$val2['username'];
				$res = MoneyLog($tmp);
			}
		}
	}
}
else
{
	//	彩票种类
	$lottery = $db->getall('SELECT * FROM '.Table('lottery').' WHERE status = 1 ORDER BY listorder DESC','id');

	//	操作的数据库
	$table = Table('lottery_order_list');
	$ptable = Table('lottery_period');
	$ptable2 = Table('lottery_play');

	//	一次10个
	$data = $db->getall('SELECT o.*,p.rednum AS result FROM '.$table.' o LEFT JOIN '.$ptable.' p ON o.iid = p.id WHERE o.status = 0 AND p.rednum <> \'\' ORDER BY o.id DESC LIMIT 10');
	foreach($data as $val)
	{
		//	开奖中
		$db->exec('UPDATE '.$table.' SET status = 99 WHERE id = '.$val['id']);
		
		//	奖金计算
		include_once(LIB_PATH.'lottery/'.$lottery[$val['cid']]['ename'].'_prize.inc.php');
		
		//	玩法
		$plays = $db->getrow('SELECT * FROM '.$ptable2.' WHERE cid = '.$val['cid'].' AND playid = '.$val['pid'].' AND status = 1');
		
		$func = 'PrizeCount_'.$val['cid'];
		$money = $func($val,$plays);
		$money = round($money,2);
		if($money > 0)	//	中奖
		{
			//	奖金记录
			$tmp = [];
			$tmp['uid']		=	$val['uid'];
			$tmp['tid']		=	'prize';
			$tmp['lkey']	=	'prize_'.$val['uid'].'_'.$val['id'];
			$tmp['money']	=	$money;
			$tmp['balance']	=	$money;
			$tmp['notes']	=	'彩票中奖：'.$lottery[$val['cid']]['title'].',ID：'.$val['id'];
			$res = MoneyLog($tmp);
			//!$res && AjaxReturn(1,'账户余额不足，请先充值');
		}
		
		//	更新订单状态
		$db->exec('UPDATE '.$table.' SET prize = '.$money.',prizetime = '.TIMESTAMP.',status = 1 WHERE id = '.$val['id']);
		
		//	追号
		if($val['tid'] > 0)
		{
			//	判断是否中奖后停止追号
			if($money > 0)
			{
				
			}
			
			$prizenum = $money > 0 ? ',prizenum = prizenum + 1' : '';
			$db->exec('UPDATE '.Table('lottery_order_track').' SET okmoney = okmoney + '.$val['money'].',oknum = oknum + 1'.$prizenum.' WHERE id = '.$val['tid']);
		}
	}
}

>>>>>>> 278f17c694d70f789a9994f7f223207d4067fb8d
exit('success');